package cn.dgut.springboot.mini.controller;

import cn.dgut.springboot.mini.entity.Account;
import cn.dgut.springboot.mini.entity.Customer;
import cn.dgut.springboot.mini.exception.CustomerException;
import cn.dgut.springboot.mini.repo.AccountRepo;
import cn.dgut.springboot.mini.repo.CustomerRepo;
import cn.dgut.springboot.mini.utils.UpdateTool;
import cn.dgut.springboot.mini.vo.CustomerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周智涛
 * @Description
 * @date 2019/12/12 11:22
 */
@RestController
@RequestMapping("/account")
@Transactional
public class AccountController {

    @Autowired
    private AccountRepo accountRepo;
    @Autowired
    private CustomerRepo customerRepo;

    @PostMapping("/{id}/accounts")
    public Account save(@PathVariable Long id,@RequestBody Account account){

        return customerRepo.findById(id).map(customer -> {
            account.setCustomer(customer);
            try {
                return accountRepo.save(account);
            }catch (Exception e){
                throw new RuntimeException("添加失败");
            }

        }).orElseThrow(()-> new RuntimeException("Customer [customerId=" +id + "] can't be found"));

    }

    @PutMapping()
    public Account update(@RequestBody Account account){

        return accountRepo.findById(account.getId()).map(customer1->{
            UpdateTool.copyNullProperties(customer1,account);
            try {
                return accountRepo.save(account);
            }catch (Exception e){
                throw new RuntimeException("更新失败");
            }

        }).orElseThrow(()->new RuntimeException("Account [accountId="+account.getId()+"] can't be found"));

    }

    @GetMapping("/{id}")
    public Account getAccountById(@PathVariable Long id){

        return accountRepo.findById(id).orElseThrow(()-> new RuntimeException(
                "Account [accountId="+id+"] can't be found"));
    }


    @DeleteMapping("/{id}")
    public ResponseEntity deleteAccount(@PathVariable Long id){

        return accountRepo.findById(id).map(account -> {
            try {
                 accountRepo.delete(account);
                 return ResponseEntity.ok().build();
            }catch (Exception e){
                throw new RuntimeException("删除失败");
            }
        }).orElseThrow(() -> new RuntimeException("Account [accountId=" + id + "] can't be found"));

    }

    /**
     * 通过客户id获取所有关联的账户，未测试通过
     */
    @GetMapping("/getAccounts/{customerId}")
    public List<Account> search(Long customerId){

        try {
            return accountRepo.findAccountsByCustomerId(customerId);
        }catch (Exception e){
            throw new RuntimeException("获取失败");
        }


    }


}
